To convert any number to a string, you can use the Str Function. The Str Function uses the following syntax

Str$(Number)

**** Note the $ that has been appended to the Str function name (and many other string functions) You do not have to do this, but if you don't, it will return a Variant rather than a string... which is another VB behavour I can't understand, as we are specifically wanting a string! VB will automatically convert this to a string, but at a performance cost (not noticable if you just do it once, but it is a good habit to get into!). You can see our Performance article for more info. ***

The following code converts the number contained in lNum, and puts it into a textbox

Dim lNum As Long
lNum = 1000
Text1.Text = Str$(lNum)

In many cases it is not necessary to convert a number to a string, as VB automatically does this for you, however it is a good practice to get into, and it is also slightly faster using the Str function, instead of letting VB do it for you. You can also use the Hex command to convert a number to a Hex string. To do this properly, use the following code. This makes all the letters lowercase, and then adds &H on to the beginning, making it a valid Hex string 

strHexString = "H&" & LCase$(Hex$(lNumber))

To get the length of a string you use the Len function. The Len Function uses the following syntax: 

Len(String)

The following code gets the text from Text1 and tells you how many characters are in it (its length)

' Declare variable
Dim lLength As Long
' Get the length of the text
lLength = Len(Text1.Text)
' Display a Message Box
Msgbox "Text1 has " & lLength & " characters in it"

To get all the text from a string you use the following syntax:

VariableOrProperty = String

For example, the following code sets Form1 caption property to the string contained in String1

' Declare the variable
Dim sString As String
' Fill the variable
sString = "Hello World"
' Assign String1 to Form1's Caption
Form1.Caption = sString

However, sometimes you will find you only want to get part of a string. The most basic functions are Left, Right and Mid. Left gets a specified number of letters from the left side of a string. Left uses the following syntax:

Left$(String, Length)

The following code will display a message box with 'Hel'. It has taken three letters from the left side of String1

' Declare the variable
Dim sString As String
' Fill the variable
sString = "Hello"
' Get the first three characters from String1
MsgBox Left$(sString, 3)

Right uses the same syntax except returns the specified number of letters from the right side of a string. The following code will display a message box with 'llo'. It has taken three letters from the right side of String1

' Declare the variable
Dim sString As String
' Fill the variable
sString = "Hello"
' Get the first three characters from String1
MsgBox Right$(sString, 3)

Mid gets a specified number of letters from a specified point in a string (counted from the left). Mid uses the following syntax:

Mid$(String, Start, Length)

The following code will display a message box with 'll'. It has taken two letters from the third character onwards from String1

' Declare the variable
Dim String1 As String
' Fill the variable
String1 = "Hello"
' Get two characters from pos 3 onwards in String1
MsgBox Mid$(String1, 3, 2)

To find if a string contains another string, you can use InStr and InStrRev (InStrRev is only in VB6). These return the first and last positions of a string contained in another string, respectively. They both use the following syntax:

InStr$(Start,StringToSearch,StringToFind, SearchMode)

Start is an optional figure representing the position to start searching the string. StringToSearch contains the string that you want to search. StringToFind contains the string that you want to find. The following code gets the text from txtFindString and highlights the first occurrence of that string in txtSearchString. vbTextCompare tells the InStr function to find matching strings, regardless of case. Use vbBinaryCompare for the search to be case sensitive.

Sub Command1_Click()
    ' Get the first occurrence of the string
    StringPos = InStr(1, txtSearchString, txtFindString, vbTextCompare)
    If StringPos = 0 Then
        ' If StringPos returns 0, then the string is not found
        Msgbox "String not found"
    Else
        ' Get the length of the string to find
        StringLen = Len(txtFindString)
        ' Set the starting position of the selection
        txtSearchString.SelStart = StringPos
        ' Set the selection length
        txtSearchString.SelLength = StringLength
    End If
End Sub

If you want to replace all occurances of a string, you can use the Replace function (only available in VB6.. you'll have to create your own if you have VB5 or lower). 

Replace(StringToSearch, StringToFind, ReplaceString, Start, Count, Compare)

The first 3 parameters speak for themselves.... The Start, Count and Compare paramters are all optional. Start specifies where to start replacing from, count specifies the maximum number of replaces, and compare specifies if it is case sensitive (vbBinaryCompare), or not (vbTextCompare).

The following code replaces all occurances of "Jane" with "Bob" in the Text1 textbox

Replace(Text1.Text, "Jane", "Bob")

To convert a string to upper case, you use the UCase$ function. The UCase$ Function uses the following syntax 

UCase$(String)

The following code converts the string contained in sString to uppercase, and puts it into a textbox

Dim sString As String
sString = "this is In Lower Case"
Text1.Text = UCase$(sNum)

To convert a string to lower case, you use the LCase$ function. The LCase$ Function uses the following syntax

LCase$(String)

The following code converts the string contained in sString to lowercase, and puts it into a textbox

Dim sString As String
sString = "this is In UPPERCASE"
Text1.Text = LCase$(sString) 

Sometimes, you will find you need to remove trailing spaces from a string. There are 3 functions for this. Trim, LTrim, RTrim. Trim removes spaces from either end of a string. LTrim removes spaces from the left end of a string. RTrim removes spaces from the right end of a string. They all use the following syntax: 

Trim(String)

The following code fills String1 with '    Hello World    ', then fills Text1, Text2, Text3, Text4 displaying the results of using nothing, Trim, LTrim and RTrim respectively

'// Declare the variable
Dim sString As String
'// Fill the variable
sString = "  Hello World   "
'// Display plain string
Text1.Text = sString
'// Display string using Trim
Text2.Text = Trim(sString)
'// Display string using LTrim
Text3.Text = LTrim(sString)
'// Display string using RTrim
Text4.Text = RTrim(sString)

When using WindowsAPI, if the function returns a string, it will always be returned with a whole load of null characters after the actual text. You can use this simple function to get rid of them! 

Function StripTerminator(ByVal strString As String) As String
    Dim intZeroPos As Integer
    intZeroPos = InStr(strString, Chr$(0))
    If intZeroPos > 0 Then
        StripTerminator = left$(strString, intZeroPos - 1)
    Else
        StripTerminator = strString
    End If
End Function

You might want to fill a string with say, 100 m's, for some strange reason (!), or fill a string with 255 spaces before being passed to a API function. If you want to fill a string with spaces, you can use the Space function: 

sString = Space(100) 

This fills sString with 100 spaces. 

If you want to fill it with another character, you use the String function. For example, the code below fills sString with 5 M's: 

sString = String(10, "M") 

Simple! 
